<!--************************************************************************************************-->
<!-- < GroupManager.php >                                                                           -->
<!-- Copyright (c) 2012-2013 San Yat-sun University School of Software                              -->
<!-- Founder : Tianjiao Li                                                                          -->
<!-- Stu : 10389129                                                                                 -->
<!-- Date: 2012-01-01                                                                             -->                                                                       
<!-- Version : 2.1                                                                                  -->
<!-- LastUpdate : 2012-01-01 13:58 BY XiaoxinMo                                                     -->
<!-- Destription : add function exist and function                                                   -->
<!--************************************************************************************************-->

<?php 
	include_once("../db/Group.php");
	include_once("../db/GroupMa.php");
	include_once("NoteManager.php");
	include_once("CommentManager.php");
	
	class GroupManager
	{
		static $instance;
		private $group;
		private $dbName = "TinyBook";
		
		public function __construct()
		{
		}
		
		public static function getInstance()
		{
			if (self::$instance == null)
				self::$instance = new self();
			return self::$instance;
		}
		
		public function insert($array,$data)
		{
			$group = new Group($array);
			$group->insert();
			
			$groupmanager = new GroupMa($data);
			$groupmanager->insert($data);
			
			return true;
		}
		
		public function delete($array)
		{
			$group = new Group($array);
			$group->delete();
		}
		
		public function update($array)
		{
			$group = new Group($array);
			$group->update();
		}
		
		// Edit By TianjiaoLi
		public function openDB()
		{
			$con = mysql_connect("localhost","root","");
			
			if (!$con)
			{
				die('Could not connect: ' . mysql_error());
			}
			
			mysql_select_db("$this->dbName",$con);
			
			return $con;
		}
		
		public function startSQL($sql,$con){
			$result = mysql_query($sql,$con);
			if (!$result)
				die('Error: '.mysql_error());
			else
				return $result;
		}
		
		public function MemberOf($data)
		{
			$con = $this->openDB();
			$antherTable = "Memberof";
			// ��ȡ����ǰ��ݵ�key�Ͷ�Ӧ��ֵ
			$temp_cols="";
			$temp_vals="";
			foreach($data as $key => $value)
			{
				$temp_cols = $temp_cols."$key,";
				$temp_vals = $temp_vals."'$value',";
			}
			$temp_cols = substr($temp_cols,0, strlen($temp_cols) - 1);
			$temp_vals = substr($temp_vals,0, strlen($temp_vals) - 1);
		
			$sql = "INSERT INTO $antherTable ($temp_cols) 
			VALUES ($temp_vals)";
				
			$this->startSQL($sql,$con);
		}
		
		// =====================added by xiaoxin
		public function getValueByGroupname($name){
			$group = new Group(null);
			return $group->findValueByGroupname($name);
		
		}
		
		public function getGrouplist($N)
		{
			$group = new Group(null);
			return $group->findNGroups($N);
		}
		
		public function getGrouplistByMaName($maName){
			$group = new Group(null);
			return $group->findGrouplistByMaName($maName);
		}
		
		public function getGroupnamesByMaName($maName, $N){
			$group = new Group(null);
			return $group->findGroupnamesByMaName($maName, $N);
		}
		
		//=====edit by Tianjiao Li @ 2013-01-07 20:33
		public function getMemberListByGroupName($groupName)
		{
			$con = $this->openDB();
			$anotherTable = "Memberof";
			
			$sql = "SELECT * FROM $anotherTable
			WHERE Groupname = '$groupName'";
			
			$list = $this->startSQL($sql,$con);
			$member_names = array();
			while($row = mysql_fetch_array($list))
				$member_names[] = $row['Username'];
				
			return $member_names;
		}
		
		public function findNMembersByGroupName($groupName, $N)
		{
			$con = $this->openDB();
			$anotherTable = "Memberof";
			
			$sql = "SELECT * FROM $anotherTable
			WHERE GroupName = '$groupName'
			LIMIT $N";
			
			return $this->startSQL($sql,$con);
		}
		
		
		public function findMemberNamesByGroupname($groupName, $N)
		{
			if ($N == null)
				$member_list = $this->getMemberListByGroupName($groupName);
			else
				$member_list = $this->findNMembersByGroupName($groupName, $N);
				
			$member_names = array();
			
			while ($member = mysql_fetch_array($member_list))
				$member_names[] = $member['Username'];
			
			return $member_names;
		}
		
		public function getNoteListByGroupName($groupName)
		{
			$member_list = $this->getMemberListByGroupName($groupName);
			$noteManager = new NoteManager();
			$noteList = array();
			foreach($member_list as $member)
			{
				$note_sql = $noteManager->getValueByUsername($member);
			}
			return $noteList;
		}
		
		public function getCommentListByGroupName($groupName)
		{
			$commentManager = new CommentManager();
			return $commentManager->getAllValue();
		}
		
		// ===============added by xiaoxin===============
		public function exist($name){
			if ($this->getValueByGroupname($name) == null)
				return false;
			return true;
		}
		
		public function getImageByGroupname($groupname){
			$img = "..\images\groups\\$groupname".".jpg";
			return $img;
		}
	}
?>